#!/bin/bash

# refer https://kubernetes.io/zh-cn/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
# If it is not a high-availability cluster, change the following IP address to the master node IP, and change port 8443 to port (6443) of API Server
MASTER02=192.168.56.12
MASTER03=192.168.56.13
APISERVER_VIP_PROXY=192.168.56.110:8443


K8S_CLUSTER_NAME=k8s-cluster

# certificate
PKI_PATH=/etc/kubernetes/pki
CA_CERT=$PKI_PATH/ca.crt
KUBE_PROXY_CERT=$PKI_PATH/kube-proxy.crt
KUBE_PROXY_KEY=$PKI_PATH/kube-proxy.key
KUBE_CONTROLLER_MANAGER_CERT=$PKI_PATH/kube-controller-manager.crt
KUBE_CONTROLLER_MANAGER_KEY=$PKI_PATH/kube-controller-manager.key
KUBE_SCHEDULER_CERT=$PKI_PATH/kube-scheduler.crt
KUBE_SCHEDULER_KEY=$PKI_PATH/kube-scheduler.key
KUBE_ADMIN_CERT=$PKI_PATH/admin.crt
KUBE_ADMIN_KEY=$PKI_PATH/admin.key

# kubeconfig
TMP_KUBECONFIG_PATH=~/k8s/kubeconfig
KUBECONFIG_PATH=/etc/kubernetes/kubeconfig

mkdir -p $TMP_KUBECONFIG_PATH
ssh vagrant@$MASTER02 "mkdir -p $TMP_KUBECONFIG_PATH"
ssh vagrant@$MASTER03 "mkdir -p $TMP_KUBECONFIG_PATH"

sudo rm -rf $KUBECONFIG_PATH && sudo mkdir -p $KUBECONFIG_PATH
ssh vagrant@$MASTER02 "sudo rm -rf $KUBECONFIG_PATH && sudo mkdir -p $KUBECONFIG_PATH"
ssh vagrant@$MASTER03 "sudo rm -rf $KUBECONFIG_PATH && sudo mkdir -p $KUBECONFIG_PATH"

echo "Add Cluster, User, Context details to the configuration file and set the current context"
KUBE_CONTROLLER_MANAGER_CONFIG=--kubeconfig=$TMP_KUBECONFIG_PATH/kube-controller-manager.kubeconfig
KUBE_PROXY_CONFIG=--kubeconfig=$TMP_KUBECONFIG_PATH/kube-proxy.kubeconfig
KUBE_SCHEDULER_CONFIG=--kubeconfig=$TMP_KUBECONFIG_PATH/kube-scheduler.kubeconfig
KUBE_ADMIN_CONFIG=--kubeconfig=$TMP_KUBECONFIG_PATH/admin.kubeconfig

echo "Configure kube-controller-manager.kubeconfig"
kubectl config $KUBE_CONTROLLER_MANAGER_CONFIG set-cluster $K8S_CLUSTER_NAME --server=https://$APISERVER_VIP_PROXY --certificate-authority=$CA_CERT --embed-certs=true   
kubectl config $KUBE_CONTROLLER_MANAGER_CONFIG set-credentials kube-controller-manager-user --client-certificate=$KUBE_CONTROLLER_MANAGER_CERT --client-key=$KUBE_CONTROLLER_MANAGER_KEY --embed-certs=true
kubectl config $KUBE_CONTROLLER_MANAGER_CONFIG set-context kube-controller-manager-context --cluster=$K8S_CLUSTER_NAME --user=kube-controller-manager-user
kubectl config $KUBE_CONTROLLER_MANAGER_CONFIG use-context kube-controller-manager-context

echo "Configure kube-proxy.kubeconfig"
kubectl config $KUBE_PROXY_CONFIG set-cluster $K8S_CLUSTER_NAME --server=https://$APISERVER_VIP_PROXY --certificate-authority=$CA_CERT --embed-certs=true
kubectl config $KUBE_PROXY_CONFIG set-credentials kube-proxy-user --client-certificate=$KUBE_PROXY_CERT --client-key=$KUBE_PROXY_KEY --embed-certs=true
kubectl config $KUBE_PROXY_CONFIG set-context kube-proxy-context --cluster=$K8S_CLUSTER_NAME --user=kube-proxy-user
kubectl config $KUBE_PROXY_CONFIG use-context kube-proxy-context

echo "Configure kube-scheduler.kubeconfig"
kubectl config $KUBE_SCHEDULER_CONFIG set-cluster $K8S_CLUSTER_NAME --server=https://$APISERVER_VIP_PROXY --certificate-authority=$CA_CERT --embed-certs=true
kubectl config $KUBE_SCHEDULER_CONFIG set-credentials kube-scheduler-user --client-certificate=$KUBE_SCHEDULER_CERT --client-key=$KUBE_SCHEDULER_KEY --embed-certs=true
kubectl config $KUBE_SCHEDULER_CONFIG set-context kube-scheduler-context --cluster=$K8S_CLUSTER_NAME --user=kube-scheduler-user
kubectl config $KUBE_SCHEDULER_CONFIG use-context kube-scheduler-context

echo "Configure admin.kubeconfig"
kubectl config $KUBE_ADMIN_CONFIG set-cluster $K8S_CLUSTER_NAME --server=https://$APISERVER_VIP_PROXY --certificate-authority=$CA_CERT --embed-certs=true
kubectl config $KUBE_ADMIN_CONFIG set-credentials kubernetes-admin-user --client-certificate=$KUBE_ADMIN_CERT --client-key=$KUBE_ADMIN_KEY --embed-certs=true
kubectl config $KUBE_ADMIN_CONFIG set-context kubernetes-admin-context --cluster=$K8S_CLUSTER_NAME --user=kubernetes-admin-user
kubectl config $KUBE_ADMIN_CONFIG use-context kubernetes-admin-context



echo "Copy Kubeconfig To Master Node"
scp $TMP_KUBECONFIG_PATH/* vagrant@$MASTER02:$TMP_KUBECONFIG_PATH
scp $TMP_KUBECONFIG_PATH/* vagrant@$MASTER03:$TMP_KUBECONFIG_PATH

sudo mv $TMP_KUBECONFIG_PATH/* $KUBECONFIG_PATH
ssh vagrant@$MASTER02 "sudo mv $TMP_KUBECONFIG_PATH/* $KUBECONFIG_PATH"
ssh vagrant@$MASTER03 "sudo mv $TMP_KUBECONFIG_PATH/* $KUBECONFIG_PATH"
